package com.zoe.onelink.core.autoconfigure;

import com.zoe.onelink.core.validation.impl.ValidationFieldInfoScanner;
import com.zoe.onelink.core.web.validation.resolver.ValidationFieldNameResolver;
import com.zoe.onelink.core.web.validation.resolver.impl.SwaggerValidationFieldNameResolver;
import com.zoe.onelink.enhancer.scanner.ClassScanner;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-11-30
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(WebMvcAutoConfiguration.EnableWebMvcConfiguration.class)
@AutoConfigureBefore(WebMvcAutoConfiguration.EnableWebMvcConfiguration.class)
@RequiredArgsConstructor
public class OnelinkWebValidationAutoConfiguration implements WebMvcConfigurer {

    @Bean
    @ConditionalOnMissingBean(ValidationFieldNameResolver.class)
    public ValidationFieldNameResolver swaggerValidationFieldNameResolver() {
        return new SwaggerValidationFieldNameResolver();
    }

    @Bean
    @ConditionalOnProperty(name = "onelink.web.translate-validation", matchIfMissing = false)
    public ClassScanner swaggerFieldInfoScanner(BeanFactory beanFactory) {
        return new ValidationFieldInfoScanner(beanFactory);
    }

}
